//
//  H_MoreInfoViewController.m
//  Honey
//
//  Created by 贾亚涵 on 16/4/9.
//  Copyright © 2016年 贾亚涵. All rights reserved.
//

#import "H_MoreInfoViewController.h"
#import "H_ChangeUserNickVC.h"
#import "H_ChangeUserPassWVC.h"

#define Btn_1 3500
#define Btn_2 3501
#define Btn_3 3502
#define Btn_4 3503
#define Btn_5 3504

@interface H_MoreInfoViewController () <UIImagePickerControllerDelegate,UINavigationControllerDelegate>
{
    UIImageView * _leftBarIV;
    UIImageView * _centerBarIV;
    UILabel * _BarLabel;
    
    UIButton * _btn1;
    UILabel * _label1;
    UIImageView * _imageV1;
    UIButton * _btn2;
    UILabel * _label2;
    UILabel * _label22;
    UIButton * _btn3;
    UILabel * _label3;
    UILabel * _label33;
    UIButton * _btn4;
    UILabel * _label4;
    UIImageView * _imageV2;
    UIImageView * _imageV3;
    UIImageView * _imageV4;
    UIImageView * _imageV5;

    
    UIButton * _btn6;
    UIButton * _btn7;

}

@end

@implementation H_MoreInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor colorWithRed:245/255.0f green:242/255.0f blue:235/255.0f alpha:1];
    
    [self initInfoView];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self infoNavigationBarBtn];
    H_UserManager * model = [H_UserManager Instance];
    H_UserMemberModel * MM = model.userModel;
    H_CoreDataDAL * dal = [H_CoreDataDAL new];
    NSMutableDictionary * mutaDic = [NSMutableDictionary dictionaryWithDictionary:[dal getUserInfoFromDataWithPhone:MM.userPhone]];
    
    _imageV1.image = [mutaDic valueForKey:@"headPic"];
    _label22.text = [mutaDic valueForKey:@"userNick"];
    _label33.text = MM.userPhone;
    
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    [_BarLabel removeFromSuperview];
    
    [_centerBarIV removeFromSuperview];
}


-(void)infoNavigationBarBtn
{
    _centerBarIV = [[UIImageView alloc] initWithFrame:CGRectMake(50, 10, 1, 20)];
    _centerBarIV.backgroundColor = [UIColor blackColor];
    _centerBarIV.alpha = 0.2f;
    [self.navigationController.navigationBar addSubview:_centerBarIV];
    
    _BarLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_centerBarIV.frame)+14, 10, 100, 20)];
    _BarLabel.text = @"个人资料";
    _BarLabel.textColor = [UIColor whiteColor];
    [self.navigationController.navigationBar addSubview:_BarLabel];
}

-(void)initInfoView
{
    
    _btn1 = [self addUIButtonWithRect:CGRectMake(0, 74, Screen_Width, 50) Tag:Btn_1];
    _label1 = [self addUILabelWithText:@"头像"];
    [_btn1 addSubview:_label1];
    _imageV1 = [[UIImageView alloc] initWithFrame:CGRectMake(Screen_Width-55, 5, 40, 40)];
    _imageV1.layer.cornerRadius = 20;
    _imageV1.layer.masksToBounds = YES;
    [_btn1 addSubview:_imageV1];
    
    _btn2 = [self addUIButtonWithRect:CGRectMake(0, CGRectGetMaxY(_btn1.frame)+1, Screen_Width, 50) Tag:Btn_2];
    _label2 = [self addUILabelWithText:@"昵称"];
    [_btn2 addSubview:_label2];
    _label22 = [[UILabel alloc] initWithFrame:CGRectMake(Screen_Width-165, 5, 130, 40)];
    _label22.font = [UIFont systemFontOfSize:15];
    _label22.textAlignment = NSTextAlignmentRight;
    [_btn2 addSubview:_label22];
    _imageV2 = [self addUIImageView];
    [_btn2 addSubview:_imageV2];

    _btn3 = [self addUIButtonWithRect:CGRectMake(0, CGRectGetMaxY(_btn2.frame)+1, Screen_Width, 50) Tag:Btn_3];
    _label3 = [self addUILabelWithText:@"手机号"];
    [_btn3 addSubview:_label3];
    
    _label33 = [[UILabel alloc] initWithFrame:CGRectMake(Screen_Width-150, 5, 120, 40)];
    _label33.font = [UIFont systemFontOfSize:15];
    [_btn3 addSubview:_label33];
    [_btn3 addSubview:_imageV3];

    _btn4 = [self addUIButtonWithRect:CGRectMake(0, CGRectGetMaxY(_btn3.frame)+1, Screen_Width, 50) Tag:Btn_4];
    _label4 = [self addUILabelWithText:@"更改密码"];
    [_btn4 addSubview:_label4];
    _imageV4 = [self addUIImageView];
    [_btn4 addSubview:_imageV4];
    
    _btn6 = [[UIButton alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_btn4.frame)+10, Screen_Width, 50)];
    _btn6.backgroundColor = [UIColor whiteColor];
    [_btn6 setTitle:@"解除关系" forState:UIControlStateNormal];
    [_btn6 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_btn6 addTarget:self action:@selector(Btn6Clicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_btn6];
    
    
    _btn7 = [[UIButton alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_btn6.frame)+1, Screen_Width, 50)];
    _btn7.backgroundColor = [UIColor whiteColor];
    [_btn7 setTitle:@"退出登录" forState:UIControlStateNormal];
    [_btn7 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_btn7 addTarget:self action:@selector(Btn7Clicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_btn7];
}

#pragma mark- UIButton
-(UIButton *)addUIButtonWithRect:(CGRect)rect Tag:(CGFloat)tag
{
    UIButton* _Btn = [[UIButton alloc] initWithFrame:rect];
    _Btn.tag = tag;
    _Btn.backgroundColor = [UIColor whiteColor];
    [_Btn addTarget:self action:@selector(BtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_Btn];
    return _Btn;
}

-(void)BtnClicked:(id)sender
{
    if (sender && [sender isKindOfClass:[UIButton class]]) {
        UIButton * btn = (UIButton *)sender;
        
        switch (btn.tag) {
            case Btn_1:
            {
                NSLog(@"touxiang");
                UIAlertController * sheetView = [UIAlertController alertControllerWithTitle:@"选择操作" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
                
                UIAlertAction * cancleAction = [UIAlertAction actionWithTitle:@"取消操作" style:UIAlertActionStyleCancel handler:nil];
                UIAlertAction * cameraAction = [UIAlertAction actionWithTitle:@"拍摄照片" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action)
                                                {
                                                    [self takePhoto];
                                                }];
                
                UIAlertAction * photoAction = [UIAlertAction actionWithTitle:@"从照片中选取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    
                    [self localPhoto];
                }];
                
                [sheetView addAction:cancleAction];
                [sheetView addAction:cameraAction];
                [sheetView addAction:photoAction];
                
                [self presentViewController:sheetView animated:YES completion:nil];
                
                
            

            }
                break;
            case Btn_2:
            {
                
                H_ChangeUserNickVC * chaN = [H_ChangeUserNickVC new];
                
                [self.navigationController pushViewController:chaN animated:YES];
                
            }
                break;
            case Btn_3:
            {
            }
                break;
            case Btn_4:
            {
                H_ChangeUserPassWVC * chaP = [H_ChangeUserPassWVC new];
                [self.navigationController pushViewController:chaP animated:YES];
            }
                break;
            default:
                break;
        }
    }
}
-(void)takePhoto
{
    UIImagePickerController * picker = [[UIImagePickerController alloc] init];
    
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    
    picker.delegate = self;
    
    picker.allowsEditing = YES;
    
    [self presentViewController:picker animated:YES completion:^{
        
    }];
}

-(void)localPhoto
{
    UIImagePickerController * picker = [[UIImagePickerController alloc] init];
    
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    
    picker.delegate = self;
    
    [self presentViewController:picker animated:YES completion:^{
        
    }];
    
}

#pragma mark- UIImagePickerControllerDelegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    [picker dismissViewControllerAnimated:YES completion:^{
        
    }];
    
    UIImage * image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    
    if (image)
    {
        float height = image.size.height/image.size.width*200;
        CGRect rect = CGRectMake(0, 0, 200, height);
        //创建一个画图的画布
        UIGraphicsBeginImageContextWithOptions(rect.size, NO, [[UIScreen mainScreen] scale] );
        
        [image drawInRect:rect];
        UIImage * image2 = UIGraphicsGetImageFromCurrentImageContext();
        //关闭画布
        UIGraphicsEndImageContext();
        NSData * data = UIImageJPEGRepresentation(image2, 1.0);
        _imageV1.image = image;
        H_CoreDataDAL * dal = [H_CoreDataDAL new];
        [dal changeUserHeadPicWithPhone:_label33.text Head:data];
    }
}

#pragma mark- UIImageView
-(UIImageView *)addUIImageView
{
    UIImageView * _ImV = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_label22.frame)+15, 15, 20, 20)];
    _ImV.image = [UIImage imageNamed:@"MoreGT"];
    
    return _ImV;
}
#pragma mark- UILabel
-(UILabel *)addUILabelWithText:(NSString *)text
{
    UILabel * _Lab = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 100, 50)];
    _Lab.text = text;
    return _Lab;
}

#pragma mark- Btn6.7
-(void)Btn6Clicked:(id)sender
{
    if (sender && [sender isKindOfClass:[UIButton class]]) {
        
        UIAlertController * alertCont1 = [UIAlertController alertControllerWithTitle:@"提示" message:@"亲，解除关系后不可恢复，三思而后行哟..." preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction * registAc1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"取消");
        }];
        UIAlertAction * certainAc1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"...");
        }];
        [alertCont1 addAction:registAc1];
        [alertCont1 addAction:certainAc1];
        [self presentViewController:alertCont1 animated:YES completion:nil];
    }
}
-(void)Btn7Clicked:(id)sender
{
    if (sender && [sender isKindOfClass:[UIButton class]]) {
        UIAlertController * alertCont2 = [UIAlertController alertControllerWithTitle:@"提示" message:@"亲，确定退出应用吗，我们将含泪恭送您，并欢迎您再次回来哟..." preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction * registAc2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"取消");
        }];
        UIAlertAction * certainAc2 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"...");
        }];
        [alertCont2 addAction:registAc2];
        [alertCont2 addAction:certainAc2];
        [self presentViewController:alertCont2 animated:YES completion:nil];

    }
}

@end
